﻿Imports System.Windows.Threading
Imports Hinshelwood.TFSStickyBuddy.Modules.Interfaces
Imports Hinshelwood.TFSStickyBuddy.Modules.PresentationModels
Imports Hinshelwood.TFSStickyBuddy.Modules.Events
Imports Microsoft.Practices.Composite.Events
Imports Microsoft.Practices.Composite.Wpf.Events

Public Class CredentialProviderPresenter
    Implements ICredentialProviderPresenter

    Private m_View As ICredentialProviderView

    Private m_Credentials As System.Net.ICredentials

    Public Sub New(ByVal view As ICredentialProviderView, ByVal eventAggregator As IEventAggregator)
        m_View = view
        eventAggregator.GetEvent(Of CredentialProvidedEvent).Subscribe(AddressOf OnRecievedCedentials, ThreadOption.BackgroundThread)
    End Sub


    Public ReadOnly Property View() As Interfaces.ICredentialProviderView Implements Interfaces.ICredentialProviderPresenter.View
        Get
            Return m_View
        End Get
    End Property

    Public Function GetCredentials(ByVal uri As System.Uri, ByVal failedCredentials As System.Net.ICredentials) As System.Net.ICredentials Implements Microsoft.TeamFoundation.Client.ICredentialsProvider.GetCredentials
        ' Not sure what to do in here

        Application.Current.Dispatcher.Invoke(New Action(Of System.Net.ICredentials)(AddressOf View.ShowView), failedCredentials)

        While m_Credentials Is Nothing
            ' Do nothing but wait
            System.Threading.Thread.Sleep(200)
        End While

        Return m_Credentials
    End Function

    Public Sub OnRecievedCedentials(ByVal cred As System.Net.ICredentials)
        m_Credentials = cred
    End Sub

    Public Sub NotifyCredentialsAuthenticated(ByVal uri As System.Uri) Implements Microsoft.TeamFoundation.Client.ICredentialsProvider.NotifyCredentialsAuthenticated
        View.CredentialsAuthenticated()
    End Sub




End Class
